package com.example.mall.web.controller;

import com.example.mall.domain.UserAddresses;
import com.example.mall.dto.UserAddressesDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.PageDTO;
import com.example.mall.query.UserAddressesQuery;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IUserAddressesService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userAddresses")
public class UserAddressesController {

    @Autowired
    public IUserAddressesService userAddressesService;

    /**
     * 分页查询用户地址表信息
     * @param userAddressesQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询用户地址表信息",notes = "分页查询用户地址表信息",httpMethod = "GET")
    public JSONResult queryPage(UserAddressesQuery userAddressesQuery){
        PageDTO<UserAddressesDto> userAddressesDtoPageDTO = userAddressesService.queryPage(userAddressesQuery);
        return JSONResult.success("查询成功",userAddressesDtoPageDTO);
    }

    /**
     * 根据id查询用户地址表信息
     * @param addressId
     * @return
     */
    @GetMapping("query/{id}")
    @ApiOperation(value = "根据id查询用户地址表信息",notes = "根据id查询用户地址表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("addressId")Long addressId){
        //1.根据id查询用户信息
        if (addressId == null){
            throw new GlobalCustomerException(ErrorEnumCode.ADDRESSID_NOT_ID);
        }
        UserAddresses userAddresses = userAddressesService.getById(addressId);
        return JSONResult.success("查询成功",userAddresses);
    }

    /**
     * 根据id查询用户地址表信息
     * @param addressId
     * @return
     */
    @GetMapping("queryUserAddressesById/{addressId}")
    @ApiOperation(value = "根据id查询用户地址表信息",notes = "根据id查询用户地址表信息",httpMethod = "GET")
    public JSONResult queryUserAddressesById(@PathVariable("addressId")Long addressId){
        //1.根据id查询用户信息
        if (addressId == null){
            throw new GlobalCustomerException(ErrorEnumCode.ADDRESSID_NOT_ID);
        }
        UserAddresses userAddresses = userAddressesService.queryUserAddressesById(addressId);
        return JSONResult.success("查询成功",userAddresses);
    }

    /**
     * 查询全部用户地址表信息
     * @param
     * @return
     */
    @GetMapping("queryUserAddressesList")
    @ApiOperation(value = "查询全部用户地址表信息",notes = "查询全部用户地址表信息",httpMethod = "GET")
    public JSONResult queryUserAddressesList(){
        List<UserAddresses> logins = userAddressesService.queryUserAddressesList();
        return JSONResult.success("查询成功",logins);
    }

    /**
     * 新增用户地址表
     * @param userAddressesDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增用户地址表",notes = "新增用户地址表",httpMethod = "POST")
    public JSONResult insert(@RequestBody UserAddressesDto userAddressesDto){
        userAddressesService.insert(userAddressesDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改用户地址表信息
     * @param userAddressesDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改用户地址表信息",notes = "修改用户地址表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody UserAddressesDto userAddressesDto){
        userAddressesService.update(userAddressesDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除用户地址表信息
     * @param addressId
     * @return
     */
    @PostMapping("delete/{id}")
    @ApiOperation(value = "删除用户地址表信息",notes = "删除用户地址表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("addressId") Long addressId) {
        userAddressesService.delete(addressId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除用户地址表信息
     * @param addressIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除用户地址表信息",notes = "批量删除用户地址表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> addressIds) {
        userAddressesService.deleteBatch(addressIds);
        return JSONResult.success("批量删除成功");
    }
}
